<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Banner;
use app\common\model\Location;
use think\Config;
use fast\UserSig;
use think\Loader;
use think\Request;
use think\Validate;

/**
 * 首页接口
 */
class Address extends Api
{
    protected $noNeedLogin = [''];//空代表都需要验证
    protected $noNeedRight = ['*'];

    /**
     * 获取地址 省份 城市
     */
    public function getProvinceList()
    {

        $id = input('id/d',0);
        $list = Location::getNextCityList($id);
        $this->success('请求成功!', ['list' => $list]);
    }

    /**
     * 获取用户地址列表
     */
    public function getUserAddressList()
    {
        $user_id = $this->auth->id;
        $list = \app\common\model\Address::getUserAddressList($user_id);
        $this->success('操作成功!', $list);
    }
    /**
     * 获取用户地址列表
     */
    public function getUserAddressListIos()
    {
        $user_id = $this->auth->id;
        $list = \app\common\model\Address::getUserAddressListIos($user_id);
        $this->success('操作成功!', $list);
    }

    /**
     * 查看用户地址
     */
    public function getAddressInfo()
    {
        $id = input('id', 0);
        if (!$id) {
            $this->error('地址不存在');
        }
        $info = \app\common\model\Address::get(['id' => $id]);
        $this->success('操作成功!', $info);

    }

    /**
     * 添加地址
     */
    public function addAddress()
    {
        $user_id = $this->auth->id;
        $data = Request::instance()->only(['name', 'phone', 'province', 'city', 'area', 'address', 'is_default', 'city_id', 'province_id', 'area_id']);
        $validate = Loader::validate('Address');
        if (!$validate->scene('add')->check($data)) {
            $this->error($validate->getError(), null, []);
        }
        if (!Validate::regex($data['phone'], "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $AddressModel = new \app\common\model\Address();
        //先查询之前是否设置过默认地址
        if ($data['is_default'] == 1) {
            $default = $AddressModel::get(['is_default' => 1, 'user_id' => $user_id]);
            if ($default) {
                $AddressModel->update(['is_default' => 0], ['id' => $default->id]);
            }
        }
        $data['user_id'] = $user_id;
        $data['create_time'] = time();
        $AddressModel->isUpdate(false)->save($data);
        $this->success('操作成功!', null);
    }

    /**
     * 删除地址
     */
    public function deleteAddress()
    {
        $user_id = $this->auth->id;
        $id = input('id/d');

        if (!$id) {
            $this->error(['code' => 10009, 'msg' => '请选择地址！']);
        }
        $AddressModel = new \app\common\model\Address();
        $data['delete_time'] = time();
        $AddressModel->update($data, ['id' => $id]);
        $this->success('操作成功!', []);
    }

    /**
     * 编辑地址
     */
    public function editAddress()
    {

        $user_id = $this->auth->id;
        $id=input('id/d');
        if (empty($id)){
            $this->error('当前地址不存在');
        }
        $data = Request::instance()->only(['name', 'phone', 'province', 'city', 'area', 'address', 'is_default', 'city_id', 'province_id', 'area_id']);
        $validate = Loader::validate('Address');
        if (!$validate->scene('add')->check($data)) {
            $this->error($validate->getError(), null, []);
        }
        if (!Validate::regex($data['phone'], "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $AddressModel = new \app\common\model\Address();
        //先查询之前是否设置过默认地址
        if ($data['is_default'] == 1) {
            $default = $AddressModel::get(['is_default' => 1, 'user_id' => $user_id]);
            if ($default) {
                $AddressModel->update(['is_default' => 0], ['id' => $default->id]);
            }
        }
        $data['user_id'] = $user_id;
        $data['create_time'] = time();
        $AddressModel->update($data,['id'=>$id]);
        $this->success('操作成功!', []);

    }


}
